<template>
  <basic-container>
    <el-form ref="userInfoFormRef"
             :model="userInfoForm"
             :rules="userInfoFormRules" label-width="100px">
      <el-form-item label="姓名" prop="userName">
        <el-input placeholder="请输入姓名" v-model="userInfoForm.userName"></el-input>
      </el-form-item>
      <el-form-item label="身份证号码" prop="userIdCard">
        <el-input placeholder="请输入身份证号码" v-model="userInfoForm.userIdCard"></el-input>
      </el-form-item>
      <el-form-item label="手机号">
        <el-input disabled v-model="userInfoForm.userId"></el-input>
      </el-form-item>
      <el-button @click="certificate">确认提交</el-button>
    </el-form>
    <el-result
        v-show="userInfoForm.userStatus === 2"
        icon="success"
        title="实名认证成功"
        sub-title="欢迎使用防伪电子印章管理系统"
    />
    <el-result
        v-show="userInfoForm.userStatus !== 2"
        icon="error"
        title="未实名认证"
        sub-title="未实名认证将无法使用本系统"
    />
  </basic-container>
</template>

<script setup>
import {certificateApi, getUserInfo} from '../../api/user';
import {ref} from 'vue';
import {ElMessage} from 'element-plus';
import {useStore} from 'vuex';

const userInfoFormRef = ref(null)
const userInfoForm = ref({
  userId: '',
  userName: '',
  userStatus: 1,
  userIdCard: '',
})
const userInfoFormRules = ref({
  userName: [{required: true, message: "请输入姓名", trigger: 'blur'}],
  userIdCard: [{required: true, message: "请输入身份证号码", trigger: 'blur'}],
})
const store = useStore()
/**
 * 获取用户信息
 */
const getUserInformation = () =>{
  getUserInfo(store.getters.userId).then(res=>{
    let data = res.data.result
    userInfoForm.value = data
    userInfoForm.value.userName = ""
  })
}
getUserInformation()

/**
 * 实名认证
 */
const certificate = () => {
  userInfoFormRef.value.validate(valid => {
    if (valid) {
      certificateApi(userInfoForm.value).then(res=>{
        let data = res.data.result
        if(data === "success"){
          ElMessage.success("实名认证成功")
        }else if(data === "failed"){
          ElMessage.error("实名认证失败，请重试！")
        }else{
          ElMessage.error(res.data.errorMsg)
        }
      })
    }
  })
}
</script>

<style scoped>

</style>
